PROGRAM CallPythonWithPipe
    IMPLICIT NONE
    INTEGER :: i, ios
    CHARACTER(LEN=100) :: line

    CALL SYSTEM('python.exe ./call_python/script.py > ./call_python/output.log')

    ! 打开文件读取数据
    OPEN(UNIT=10, FILE="./call_python/output.log", STATUS="OLD", ACTION="READ", IOSTAT=ios)
    
    IF (ios /= 0) THEN
      PRINT *, "Failed to read output file."
      STOP
    END IF
    
    DO
      READ(10, '(A)', IOSTAT=ios) line
      IF (ios /= 0) EXIT
      READ(line, *) i
      PRINT *, i
    END DO
    
    CLOSE(10)
    ! ! 打开管道，通过 Python 输出流读取数据
    ! OPEN(UNIT=10, FILE='COMMAND="D:/Install/Anaconda3/python.exe E:/Projects/C3D10M/repo/fortran-study/call_python/script.py"', ACTION="READ", STATUS="OLD", IOSTAT=ios)
  
    ! ! 检查是否成功打开
    ! IF (ios /= 0) THEN
    !   PRINT *, "Failed to execute Python script. IOSTAT =", ios
    !   STOP
    ! END IF
  
    ! ! 逐行读取 Python 脚本输出
    ! DO
    !   READ(10, '(A)', IOSTAT=ios) line
    !   IF (ios /= 0) EXIT
    !   READ(line, *) i  ! 将读取到的字符串转换为整数
    !   PRINT *, "Read from Python:", i
    ! END DO
  
    ! CLOSE(10)
  END PROGRAM
  